package com.yunshipei.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.yunshipei.core.utils.SystemUtils;
import com.yunshipei.enterplorer.R;
import com.yunshipei.utils.BaseUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import io.rong.eventbus.EventBus;

public class MenuFragment extends Fragment {

    private static final String ARGS_USER_NAME = "com.enterplorer.user.name";
    private static final String ARGS_USER_IMAGE = "com.enterplorer.user.image";

    private Context mContext;

    @Bind(R.id.user_avator)
    protected CircleImageView ciAvatar;
    @Bind(R.id.browser_menu_user)
    protected TextView tvUserName;
    @Bind(R.id.browser_refresh)
    protected TextView tvRefresh;
    @Bind(R.id.browser_pc_version)
    protected TextView tvOpenPC;
    @Bind(R.id.browser_setting)
    protected TextView tvSettings;
    @Bind(R.id.close_menu_button)
    protected ImageView ivCloseMore;
    @Bind(R.id.browser_back)
    protected ImageView ivBack;
    @Bind(R.id.browser_go_ahead)
    protected ImageView ivForward;
    @Bind(R.id.more_settings)
    protected ImageView ivMoreMenu;
    @Bind(R.id.browser_home)
    protected ImageView ivHome;
    @Bind(R.id.main_group_liner)
    protected FrameLayout flMessage;
    @Bind(R.id.de_num)
    protected TextView tvMessageNum;
    @Bind(R.id.ll_menu_extra)
    protected LinearLayout llMenuExtraContainer;
    @Bind(R.id.browser_menu_shadow)
    protected View vShadow;
    @Bind(R.id.browser_share)
    protected TextView tvShare;
    @Bind(R.id.container_menu_2)
    protected LinearLayout mMenuSecondView;
    @Bind(R.id.browser_font_size)
    protected TextView btnFontSize;

    private String userName;
    private String userImage;

    private OnFragmentInteractionListener mFragmentInteractionListener;

    public static MenuFragment newInstance(String userName, String userImage) {
        MenuFragment menuFragment = new MenuFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARGS_USER_NAME, userName);
        bundle.putString(ARGS_USER_IMAGE, userImage);
        menuFragment.setArguments(bundle);
        return menuFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = getActivity();
        Fragment fragment = getParentFragment();
        if (fragment != null && fragment instanceof OnFragmentInteractionListener) {
            mFragmentInteractionListener = (OnFragmentInteractionListener) fragment;
        }
        Bundle bundle = getArguments();
        this.userName = bundle.getString(ARGS_USER_NAME);
        this.userImage = bundle.getString(ARGS_USER_IMAGE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_browser_more, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvUserName.setText(TextUtils.isEmpty(userName) ? "未知" : userName);
        loadAvatar(userImage);
        llMenuExtraContainer.setVisibility(View.INVISIBLE);
        if (SystemUtils.isPad(mContext)) {
            flMessage.setVisibility(View.GONE);
            mMenuSecondView.setVisibility(View.GONE);
            tvSettings.setVisibility(View.GONE);
        }
    }

    /**
     * 隐藏或显示菜单的更多功能
     *
     * @param hide 是否隐藏
     */
    public void hideMoreMenu(boolean hide) {
        vShadow.setVisibility(hide ? View.INVISIBLE : View.VISIBLE);
        vShadow.startAnimation(hide ? AnimationUtils.loadAnimation(mContext, R.anim.alpha_out) : AnimationUtils.loadAnimation(mContext, R.anim.alpha_in));
        llMenuExtraContainer.setVisibility(hide ? View.INVISIBLE : View.VISIBLE);
        llMenuExtraContainer.startAnimation(hide ? AnimationUtils.loadAnimation(mContext, R.anim.slide_out_to_bottom) : AnimationUtils.loadAnimation(mContext, R.anim.slide_in_from_bottom));
        if (mFragmentInteractionListener != null) {
            mFragmentInteractionListener.onMoreMenuHide(hide);
        }
    }

    @OnClick({R.id.browser_refresh
            , R.id.browser_pc_version
            , R.id.browser_setting
            , R.id.browser_back
            , R.id.browser_go_ahead
            , R.id.browser_home
            , R.id.main_group_liner
            , R.id.browser_menu_shadow
            , R.id.more_settings
            , R.id.close_menu_button
            , R.id.browser_share
            , R.id.browser_font_size})
    protected void onClickEvent(View view) {
        if (!BaseUtil.isFastDoubleClick() && mFragmentInteractionListener != null) {
            if (isMoreMenuVisible()) {
                hideMoreMenu(true);
            }
            int id = view.getId();
            switch (id) {
                case R.id.browser_refresh: //刷新
                    mFragmentInteractionListener.refresh();
                    break;
                case R.id.browser_pc_version://电脑版
                    mFragmentInteractionListener.openPC();
                    break;
                case R.id.browser_setting://设置
                    mFragmentInteractionListener.settings();
                    break;
                case R.id.browser_back://后退返回
                    mFragmentInteractionListener.goBack();
                    break;
                case R.id.browser_go_ahead://前进
                    mFragmentInteractionListener.goFroward();
                    break;
                case R.id.browser_home://返回首页
                    mFragmentInteractionListener.home();
                    break;
                case R.id.main_group_liner://返回消息列表
                    mFragmentInteractionListener.message();
                    break;
                case R.id.browser_menu_shadow://更多菜单弹起的阴影背景
                    if (isMoreMenuVisible()) {
                        hideMoreMenu(true);
                    }
                    break;
                case R.id.more_settings://打开更多菜单
                    if (!isMoreMenuVisible()) {
                        hideMoreMenu(false);
                    }
                    break;
                case R.id.close_menu_button://隐藏更多菜单
                    if (isMoreMenuVisible()) {
                        hideMoreMenu(true);
                    }
                    break;
                case R.id.browser_share://分享
                    mFragmentInteractionListener.share(tvShare);
                    break;
                case R.id.browser_font_size: //字体修改
                    mFragmentInteractionListener.showFontSizeSetting();
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (isMoreMenuVisible()) {
            hideMoreMenu(true);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 设置打开PC版按钮是否可用
     */
    public void setOpenPCBtnEnable(boolean enable) {
        tvOpenPC.setEnabled(enable);
        tvOpenPC.setAlpha(enable ? 1.0f : 0.5f);
    }

    /**
     * 设置返回按钮是否可用
     */
    public void setBackBtnEnable(boolean enable) {
        ivBack.setEnabled(enable);
        ivBack.setAlpha(enable ? 1.0f : 0.5f);
    }

    /**
     * 设置前进按钮是否可用
     */
    public void setForwardBtnEnable(boolean enable) {
        ivForward.setEnabled(enable);
        ivForward.setAlpha(enable ? 1.0f : 0.5f);
    }

    /**
     * 设置分享按钮的状态是否可用
     */
    public void setShareBtnEnable(boolean enable) {
        tvShare.setEnabled(enable);
        tvShare.setAlpha(enable ? 1.0f : 0.5f);
    }

    /**
     * 设置home键是否可用
     */
    public void setHomeBtnEnable(boolean enable) {
        ivHome.setEnabled(enable);
        ivHome.setAlpha(enable ? 1.0f : 0.5f);
    }

    /**
     * 设置字体大小按钮是否可用
     */
    public void setFontSizeBtnEnable(boolean enable) {
        btnFontSize.setEnabled(enable);
        btnFontSize.setAlpha(enable ? 1.0f : 0.5f);
    }

    /**
     * 当前菜单更多菜单布局是否显示
     */
    public boolean isMoreMenuVisible() {
        return llMenuExtraContainer.getVisibility() == View.VISIBLE;
    }

    /**
     * 隐藏弹出的更多菜单内容
     */
    public void hideExtraMenu() {
        if (isMoreMenuVisible()) {
            hideMoreMenu(true);
        }
    }

    public void updateUnreadMsgNum(int count) {
        if (tvMessageNum != null) {
            if (count == 0) {
                tvMessageNum.setVisibility(View.GONE);
            } else if (count > 0 && count < 100) {
                tvMessageNum.setVisibility(View.VISIBLE);
                tvMessageNum.setText(String.valueOf(count));
            } else {
                tvMessageNum.setVisibility(View.VISIBLE);
                tvMessageNum.setText(R.string.no_read_message);
            }
        }
    }

    public void updateUserImage(String userImage) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            bundle.putString(ARGS_USER_IMAGE, userImage);
        }
        loadAvatar(userImage);
    }

    /**
     * 加载个人头像
     */
    private void loadAvatar(String imageUrl) {
//        Glide.with(mContext).load(imageUrl).placeholder(R.drawable.icon_unknown_user).listener(new RequestListener<String, GlideDrawable>() {
//            @Override
//            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
//                return false;
//            }
//
//            @Override
//            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
//                ciAvatar.setImageDrawable(resource);
//                return true;
//            }
//        }).into(ciAvatar);
        Glide.with(mContext).load(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher).into(ciAvatar);
    }

    interface OnFragmentInteractionListener {
        /**
         * 浏览器前进
         */
        void goFroward();

        /**
         * 浏览器后退
         */
        void goBack();

        /**
         * 浏览器刷新
         */
        void refresh();

        /**
         * 浏览器进入主页
         */
        void home();

        /**
         * 浏览器进入消息列表
         */
        void message();

        /**
         * 浏览器适配页面打开PC版
         */
        void openPC();

        /**
         * 浏览器设置
         */
        void settings();

        /**
         * 分享
         */
        void share(View view);

        /**
         * 打开更多菜单
         *
         * @param hide 状态为隐藏
         */
        void onMoreMenuHide(boolean hide);

        /**
         * 设置字体
         */
        void showFontSizeSetting();
    }
}
